package cnt5106.p2p.common.protocol;

public enum MessageType {
	TRACKER_REGISTER_PEER((byte) 0x00),
	TRACKER_REGISTER_GROUP((byte) 0x01),
	TRACKER_LIST((byte) 0x02),
	TRACKER_REPLY_PEER((byte) 0x03),
	TRACKER_REPLY_GROUP((byte) 0x04),
	TRACKER_REPLY_LIST((byte) 0x05),
	TRACKER_NOTIFY_NEW_PEER((byte) 0x06),
	TRACKER_DUPLICATE_FILENAME((byte) 0x0A),
	TRACKER_FILE_NOT_FOUND((byte) 0x0B),
	PEER_REQUEST_CHUNK((byte) 0x10),
	PEER_POSITIVE_REPLY((byte) 0x11),
	PEER_NEGATIVE_REPLY((byte) 0x12),
	PEER_FILEOWNER_DATAPACKET((byte) 0x20),
	PEER_FILEOWNER_END((byte) 0x21),
	PEER_TCPPORT_REQUEST((byte) 0x22),
	PEER_TCPPORT_REPLY((byte) 0x23),
	PEER_FINISH((byte) 0x24),
	PEER_DATA_ACK((byte) 0x25),
	UNKNOWN_MESSAGE_TYPE((byte) 0xFF);
	
	private MessageType(byte code) {
		this.code = code;
	}
	
	private final byte code;

	public byte getCode() {
		return code;
	}
	
	static MessageType getMessageType(byte code) {
		switch (code) {
		case 0x00: return MessageType.TRACKER_REGISTER_PEER; 
		case 0x01: return MessageType.TRACKER_REGISTER_GROUP;
		case 0x02: return MessageType.TRACKER_LIST;
		case 0x03: return MessageType.TRACKER_REPLY_PEER;
		case 0x04: return MessageType.TRACKER_REPLY_GROUP;
		case 0x05: return MessageType.TRACKER_REPLY_LIST;
		case 0x06: return MessageType.TRACKER_NOTIFY_NEW_PEER;
		case 0x0A: return MessageType.TRACKER_DUPLICATE_FILENAME;
		case 0x0B: return MessageType.TRACKER_FILE_NOT_FOUND;
		case 0x10: return MessageType.PEER_REQUEST_CHUNK;
		case 0x11: return MessageType.PEER_POSITIVE_REPLY;
		case 0x12: return MessageType.PEER_NEGATIVE_REPLY;
		case 0x20: return MessageType.PEER_FILEOWNER_DATAPACKET;
		case 0x21: return MessageType.PEER_FILEOWNER_END;
		case 0x22: return MessageType.PEER_TCPPORT_REQUEST;
		case 0x23: return MessageType.PEER_TCPPORT_REPLY;
		case 0x24: return MessageType.PEER_FINISH;
		case 0x25: return MessageType.PEER_DATA_ACK;
		default: return MessageType.UNKNOWN_MESSAGE_TYPE;
		}
	}
}
